using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowParticipantsData = Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData;

namespace Scenario.Modeler
{
	/// <summary>
	/// Descrizione di riepilogo per FormAttributes.
	/// </summary>
	public class Participants : System.Windows.Forms.Form
	{
		public WorkflowParticipantsData.XMLRoles xmlRoles;
		public WorkflowParticipantsData.XMLRoles xmlRolesSelected;

		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnModify;
		private System.Windows.Forms.Button btnRemove;
		private System.Windows.Forms.ListView listRoles;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Participants(WorkflowParticipantsData.XMLRoles XmlRoles)
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//

			listRoles.Clear();
			listRoles.Columns.Add("Key",100, HorizontalAlignment.Left);
			listRoles.Columns.Add("Name",listRoles.Width - 100, HorizontalAlignment.Left);
			if ( XmlRoles == null )
			{
				xmlRoles = new WorkflowParticipantsData.XMLRoles();
			}
			else
			{
				xmlRoles = XmlRoles;
				for ( int i=0; i<xmlRoles.Count; i++)
				{
					WorkflowParticipantsData.XMLRole xmlRole = (WorkflowParticipantsData.XMLRole)xmlRoles[i];

					ListViewItem item = new ListViewItem( new string[] { xmlRole.Key , xmlRole.Name});
					listRoles.Items.Add (item);
				}
			}

			xmlRolesSelected = null;
		}

		public Participants(WorkflowParticipantsData.XMLRoles XmlRoles, WorkflowParticipantsData.XMLRoles xmlRolesSelected)
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();
			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//

			listRoles.Clear();
			listRoles.Columns.Add("Key",100, HorizontalAlignment.Left);
			listRoles.Columns.Add("Name",listRoles.Width - 100, HorizontalAlignment.Left);
			if ( XmlRoles == null )
			{
				xmlRoles = new WorkflowParticipantsData.XMLRoles();
			}
			else
			{
				xmlRoles = XmlRoles;
				for ( int i=0; i<xmlRoles.Count; i++)
				{
					WorkflowParticipantsData.XMLRole xmlRole = (WorkflowParticipantsData.XMLRole)xmlRoles[i];

					ListViewItem item = new ListViewItem( new string[] { xmlRole.Key , xmlRole.Name});
					listRoles.Items.Add (item);

					try
					{
						xmlRolesSelected.GetRole (xmlRole.Key);
						item.Checked = true;
					}
					catch ( Scenario.WorkflowEngine.DataModel.Exception.RoleNotFound )
					{
					}
				}
			}

			listRoles.CheckBoxes = true;
		}

		public WorkflowParticipantsData.XMLRoles XMLRoles
		{
			get { return xmlRoles; }
		}

		public WorkflowParticipantsData.XMLRoles XMLRolesSelected
		{
			get { return xmlRolesSelected; }
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnRemove = new System.Windows.Forms.Button();
            this.btnModify = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.listRoles = new System.Windows.Forms.ListView();
            this.panel2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 641);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(1016, 42);
            this.panel2.TabIndex = 15;
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(453, 7);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(60, 35);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(327, 7);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(66, 35);
            this.btnSave.TabIndex = 9;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnRemove);
            this.panel1.Controls.Add(this.btnModify);
            this.panel1.Controls.Add(this.btnAdd);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 593);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(1016, 48);
            this.panel1.TabIndex = 16;
            // 
            // btnRemove
            // 
            this.btnRemove.Location = new System.Drawing.Point(227, 14);
            this.btnRemove.Name = "btnRemove";
            this.btnRemove.Size = new System.Drawing.Size(73, 21);
            this.btnRemove.TabIndex = 2;
            this.btnRemove.Text = "Remove";
            this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
            // 
            // btnModify
            // 
            this.btnModify.Location = new System.Drawing.Point(147, 14);
            this.btnModify.Name = "btnModify";
            this.btnModify.Size = new System.Drawing.Size(66, 21);
            this.btnModify.TabIndex = 1;
            this.btnModify.Text = "Modify";
            this.btnModify.Click += new System.EventHandler(this.btnModify_Click);
            // 
            // btnAdd
            // 
            this.btnAdd.Location = new System.Drawing.Point(67, 14);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(66, 21);
            this.btnAdd.TabIndex = 0;
            this.btnAdd.Text = "Add";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // listRoles
            // 
            this.listRoles.Location = new System.Drawing.Point(7, 14);
            this.listRoles.Name = "listRoles";
            this.listRoles.Size = new System.Drawing.Size(826, 471);
            this.listRoles.TabIndex = 17;
            this.listRoles.UseCompatibleStateImageBehavior = false;
            this.listRoles.View = System.Windows.Forms.View.Details;
            this.listRoles.DoubleClick += new System.EventHandler(this.listRoles_DoubleClick);
            // 
            // Participants
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1016, 683);
            this.Controls.Add(this.listRoles);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.panel2);
            this.Name = "Participants";
            this.Text = "Partecipants";
            this.panel2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if ( listRoles.CheckBoxes )
			{
				xmlRolesSelected = new WorkflowParticipantsData.XMLRoles();

				for ( int i=0; i<listRoles.CheckedItems.Count; i++)
				{
					WorkflowParticipantsData.XMLRole xmlRole = new WorkflowParticipantsData.XMLRole();
					xmlRole.Key = listRoles.CheckedItems[i].SubItems[0].Text;

					xmlRolesSelected.Add (xmlRole);
				}
			}
			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			xmlRoles = null;
			xmlRolesSelected = null;
			this.Close();
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			FormParticipant frmRole = new FormParticipant();
			frmRole.ShowDialog (this);

			if ( frmRole.xmlRole != null )
			{
				ListViewItem item = new ListViewItem( new string[] { frmRole.xmlRole.Key, frmRole.xmlRole.Name,});
				listRoles.Items.Add (item);
				xmlRoles.Add ( frmRole.xmlRole);
			}
		}

		private void btnModify_Click(object sender, System.EventArgs e)
		{
			if ( listRoles.SelectedItems.Count == 0 )
			{
				MessageBox.Show ("Select one role.");
				return;
			}
			else if ( listRoles.SelectedItems.Count >1 )
			{
				MessageBox.Show ("Select only one role.");
				return;
			}

			WorkflowParticipantsData.XMLRole xmlRole = this.GetRole (listRoles.SelectedItems[0]);

			FormParticipant frmRole = new FormParticipant(xmlRole);
			frmRole.ShowDialog ();

			if ( frmRole.xmlRole != null )
			{
				listRoles.SelectedItems[0].SubItems[0].Text = frmRole.xmlRole.Key;
				listRoles.SelectedItems[0].SubItems[1].Text = frmRole.xmlRole.Name;

				xmlRole.Parse (frmRole.xmlRole.GetXML());
			}
		}

		private void btnRemove_Click(object sender, System.EventArgs e)
		{
			if ( listRoles.SelectedItems.Count == 0 )
			{
				MessageBox.Show ("Select one role.");
				return;
			}

			for ( int i=0; i<listRoles.SelectedItems.Count;i++)
			{
				xmlRoles.RemoveRole ( listRoles.SelectedItems[i].SubItems[0].Text);
				listRoles.Items.RemoveAt ( listRoles.SelectedItems[i].Index);
			}
		}

		private WorkflowParticipantsData.XMLRole GetRole(ListViewItem item)
		{
			return xmlRoles.GetRole ( item.SubItems[0].Text);
		}

		private void listRoles_DoubleClick(object sender, System.EventArgs e)
		{
			this.btnModify_Click(listRoles, null);
		}

	}
}
